package com.it.glide.glidesample;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import static com.it.glide.glidesample.R.id.iv;
import static com.it.glide.glidesample.R.id.iv2;

public class MainActivity extends AppCompatActivity {

    private ImageView imageView;


    private ImageView imageView2;
    private RequestManager requestManager;
    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(iv);
        imageView2 = (ImageView) findViewById(iv2);
//        "http://imgsrc.baidu.com/forum/pic/item/cefc1e178a82b9017ad72597738da9773912ef18.jpg"
      url="http://p1.pstatp.com/large/166200019850062839d3";    //"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1497186501&di=924f72e34d4a819157d452a829c4a671&imgtype=jpg&er=1&src=http%3A%2F%2Fpic.baike.soso.com%2Fp%2F20111019%2Fext-20111019161245-518276471.jpg";
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 必须在后台线程中调用，建议同时clearMemory()
                Glide.get(MainActivity.this).clearDiskCache();
            }
        }).start();

        requestManager = Glide.with(MainActivity.this);
//         requestManager.load(new WebServiceGlideUrl(""))

    }


    public void onClick(View view) {
        imageView.post(new Runnable() {
            @Override
            public void run() {
                BitmapDrawable bd   = (BitmapDrawable) imageView.getDrawable();
                if (bd != null) {
                    Bitmap image  =    bd.getBitmap();
                    System.out.println("__height:"+image.getHeight()+"width:"+image.getWidth());
                }

            }
        });
        requestManager.
                load(url)
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
//.override(300,300)
                .dontAnimate()
                .fitCenter()
               /* .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {

                        imageView.post(new Runnable() {
                            @Override
                            public void run() {
                                GlideBitmapDrawable bd   = (GlideBitmapDrawable) imageView.getDrawable();
                                if (bd != null) {
                                    Bitmap image  =    bd.getBitmap();
                                    System.out.println("__height:"+image.getHeight()+"width:"+image.getWidth());
                                }

                            }
                        });
                        return false;
                    }


                })*/
                .into(imageView2);


           startActivity(new Intent(this.getApplicationContext(),GlideListActivity.class));




 /* new Handler().postDelayed(new Runnable() {
      @Override
      public void run() {*/
          /*requestManager.
                  load("http://imgsrc.baidu.com/forum/pic/item/cefc1e178a82b9017ad72597738da9773912ef18.jpg")

                  .skipMemoryCache(true)
                  .diskCacheStrategy(DiskCacheStrategy.ALL)
                  .dontAnimate()
                  .into(imageView2);*/

    /*      startActivity(new Intent(MainActivity.this,FirstActivity.class));
     finish();*/
//      }
//  },10000);


   /* new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            BitmapDrawable drawable = (BitmapDrawable)imageView2.getDrawable();
            System.out.println(drawable.getBitmap().getWidth()+drawable.getBitmap().getHeight());
        }
    },10000);*/

//17.70   18.62

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        System.out.println("___onDestroy_______");
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();

    }
}
